#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

using PdfCreative.Pdf.Writer;

namespace PdfCreative.Pdf.Graphics
{
    /// <summary>
    /// The StringFormat class encapsulates string rendering options. When text is drawn using the Graphics class,
    /// the StringFormat determines the alignment and appearance of the text.
    /// </summary>
    public class StringFormat
    {
        // Private fields
        // ==============
        #region Private fields
        private float _characterSpacing;                        // The space between characters.
        private HorizontalAlignment _horizontalAlignment;       // The horizontal alignment.
        private float _lineSpacing;                             // The space between lines.
        private float _textRatio;                               // The aspect ratio of text.
        // private Trimming _trimming;                          // String trimming options.
        private VerticalAlignment _verticalAlignment;           // The vertical alignment.
        private float _wordSpacing;                             // The space between words.
        #endregion



        // Public properties
        // =================
        #region Public properties
        /// <summary>
        /// Sets or retrieves the character spacing, in points. The default value is 0. Negative values are allowed.
        /// </summary>
        public float CharacterSpacing
        {
            get { return _characterSpacing; }
            set { _characterSpacing = value; }
        }

        /// <summary>
        /// Sets or retrieves the horizontal alignment of this PdfStringFormat instance.
        /// </summary>
        public HorizontalAlignment HorizontalAlignment
        {
            get { return _horizontalAlignment; }
            set { _horizontalAlignment = value; }
        }

        /// <summary>
        /// Sets or retrieves line spacing, in points. The default is 0. Negative values are allowed.
        /// </summary>
        public float LineSpacing
        {
            get { return _lineSpacing; }
            set { _lineSpacing = value; }
        }

        /// <summary>
        /// Sets or retrieves the text ratio. The ratio must be larger than 0. The default value is 1.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">The supplied ratio was 0 or a negative number.</exception>
        public float TextRatio
        {
            get { return _textRatio; }
            set {
                if (_textRatio <= 0)
                    throw new ArgumentOutOfRangeException("value", "ratio must be a positive value");
                _textRatio = value;
            }
        }

        /*
        /// <summary>
        /// Sets or retrieves the string trimming settings of this PdfStringFormat instance.
        /// </summary>
        public Trimming Trimming
        {
            get { return _trimming; }
            set { _trimming = value; }
        }
        */

        /// <summary>
        /// Sets or retrieves the vertical alignment of this PdfStringFormat instance.
        /// </summary>
        public VerticalAlignment VerticalAlignment
        {
            get { return _verticalAlignment; }
            set { _verticalAlignment = value; }
        }

        /// <summary>
        /// Sets or retrieves the word spacing, in points. The default value is 0. Negative values are allowed.
        /// </summary>
        public float WordSpacing
        {
            get { return _wordSpacing; }
            set { _wordSpacing = value; }
        }

        #endregion



        // Public methods
        // ==============
        #region Public methods
        /// <summary>
        /// Clones this StringFormat instance.
        /// </summary>
        /// <returns>The cloned instance.</returns>
        public StringFormat Clone()
        {
            StringFormat clone = new StringFormat();

            clone._characterSpacing = this._characterSpacing;
            clone._horizontalAlignment = this._horizontalAlignment;
            clone._lineSpacing = this._lineSpacing;
            clone._textRatio = this._textRatio;
            clone._verticalAlignment = this._verticalAlignment;
            clone._wordSpacing = this._wordSpacing;

            return clone;
        }

        #endregion



        // Base class overrides
        // ====================
        #region Object overrides
        /// <summary>
        /// Indicates if the two instances are equal.
        /// </summary>
        /// <param name="obj">The other instance.</param>
        /// <returns>True if the two instances are equal. False otherwise.</returns>
        public override bool Equals(object obj)
        {
            bool result = false;

            StringFormat otherFormat = obj as StringFormat;
            if (null != otherFormat)
                result =
                    this._characterSpacing == otherFormat._characterSpacing &&
                    this._horizontalAlignment == otherFormat._horizontalAlignment &&
                    this._lineSpacing == otherFormat._lineSpacing &&
                    this._textRatio == otherFormat._textRatio &&
                    this._verticalAlignment == otherFormat._verticalAlignment &&
                    this._wordSpacing == otherFormat._wordSpacing;

            return result;
        }

        /// <summary>
        /// Retrieves the hash code for this string format.
        /// </summary>
        /// <returns>The hash code</returns>
        public override int GetHashCode()
        {
            int hashCode = 0;

            hashCode ^= _characterSpacing.GetHashCode();
            hashCode ^= _horizontalAlignment.GetHashCode();
            hashCode ^= _lineSpacing.GetHashCode();
            hashCode ^= _textRatio.GetHashCode();
            hashCode ^= _verticalAlignment.GetHashCode();
            hashCode ^= _wordSpacing.GetHashCode();

            return hashCode;
        }
        #endregion



        // Internal implementation
        // =======================
        #region Internal implementation
        /// <summary>
        /// Writes the changes between this format and the specified other format.
        /// </summary>
        /// <param name="writer">The writer to use.</param>
        /// <param name="otherFormat">The other format.</param>
        internal void WriteChanges(PdfWriter writer, StringFormat otherFormat)
        {
            if (otherFormat.CharacterSpacing != this.CharacterSpacing)
                writer.SetCharacterSpacing(this.CharacterSpacing);

            if (otherFormat.WordSpacing != this.WordSpacing)
                writer.SetWordSpacing(this.WordSpacing);

            if (otherFormat.TextRatio != this.TextRatio)
                writer.SetFontRatio((int)(this.TextRatio * 100));
        }
        #endregion



        // Public constructor
        // ==================
        #region Public constructor
        /// <summary>
        /// Constructs a new PdfStringFormat instance.
        /// </summary>
        public StringFormat()
        {
            _horizontalAlignment = HorizontalAlignment.Left;
            _verticalAlignment = VerticalAlignment.Top;
            // _trimming = Trimming.NoTrim;
            _textRatio = 1f;
        }
        #endregion

    } // PdfStringFormat class
}
